//
//  MFCyclmage.m
//  ScrollView轮播图-封装
//
//  Created by mafeng on 16/7/21.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "MFCyclmage.h"

@interface MFCyclmage ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *imagePage;
@property (nonatomic, strong) NSArray *imagesArray;
@end

@implementation MFCyclmage
#pragma mark -- scrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 改变pageController的对应下标
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _imagePage.currentPage = page - 1;
    if (page == 0) {
        // 改变滚动视图的下标
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
        _imagePage.currentPage = _imagePage.numberOfPages - 1;
    } else if (page == _imagesArray.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagePage.currentPage = 0;
    }
}
#pragma mark - initWithFrame
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$%%
        // 子控件初始化时, frame不要与初始化方法的frame一样
        // 推荐使用self.bounds / CGRectMake(0, 0, frame.size.width, self.size.height)
        //%%%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:_cycleScrollView];
        [self initCycleScrollView];
        [self createImagePage:frame];
    }
    return self;
}
- (void)initCycleScrollView {
    _cycleScrollView.backgroundColor = [UIColor blackColor];
    _cycleScrollView.bounces = NO;
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    _cycleScrollView.delegate = self;
}
- (void)createImagePage:(CGRect)frame {
    _imagePage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
    [self addSubview:_imagePage];
    _imagePage.numberOfPages = 5;
    _imagePage.center = CGPointMake(frame.size.width / 2, frame.size.height - 20 / 2);
    [_imagePage addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
}
- (void)imagePageChange:(UIPageControl *)page {
    [_cycleScrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}



#pragma mark -- ImageArray
- (void)setImageWithArray:(NSArray *)array {
    // 根据传过来的数组, 进行处理, 生成图片数组
    NSArray *imageArray = [self handleArray:array];
    // 逻辑判断时, 用到的数组内容
    self.imagesArray = [NSArray arrayWithArray:imageArray];
    // 根据图片数组, 生成对应得imageView
    [self createAllImagesWithImageArray:imageArray];
    // 根据图片数组生成对应的视图
    _imagePage.numberOfPages = array.count;
    _cycleScrollView.contentSize = CGSizeMake(imageArray.count * self.bounds.size.width, 0);
    // 因为添加了最后一张图, 要让轮播图看起来在第一张
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);

    }
- (NSArray *)handleArray:(NSArray *)array {
    // 判断array元素的数据类型(不知道别人使用你的类的时候传进来的类型是什么)
    id firstObject = [array firstObject];
    // 最终的图片数组
    NSMutableArray *imageArray = [NSMutableArray array];
    // 判断对象是不是某种类型 isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把 字符串 数组 转成 图片 数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imageArray addObject:image];
        }
        
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        // 这个方法用不可变数组将自己转为可变数组
        [imageArray setArray:array];
    }
    // 获取到第一张图
    UIImage *firstimage = [imageArray firstObject];
    // 获取到最后一张图
    UIImage *lastImage = [imageArray lastObject];
    // 下标为0的位置放最后一张图
    [imageArray insertObject:lastImage atIndex:0];
    // 在最后添加一张图
    [imageArray addObject:firstimage];
    // 用copy可以直接将可变的数组变为不可变的, 类比mutablecopy
    return [imageArray copy];
}
- (void)createAllImagesWithImageArray:(NSArray *)imageArray {
    // 使用数组创建imageView
    for (int i = 0; i < imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imageArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}
@end
